<?php
/**--------------------
 * Date: 2018/2/1
 * FileName:Sort.php 排行
 * ProjectName:ZhiShiRongYao
 * -------------------- */


namespace app\api\controller;

use app\api\ApiBase;
use think\Db;

class Sort extends ApiBase
{
    /**
     *获取排行列表
     *
     * @param string $type 类型 friend|world
     *
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException 数据库报错
     */
    public function getSortList($type = 'world')
    {
        $typeArr = [
            'friend',
            'world'
        ];
        if (in_array($type, $typeArr)) {

            if ($type === 'friend') {
                $db = Db::name('user u')
                        ->distinct('u.id')
                        ->field('u.id,city,user_grad_logo,u.display_name as user_name,avatar,grad.name,grad.id as g_id,curr_grad_start')
                        ->join('friends f', 'u.id=f.to_user_id OR u.id='.$this->user_info['id'])
                        ->join('grad', 'grad.id=u.grad_id')
                        ->where('f.user_id', $this->user_info['id'])
                        ->order([
                                    'grad_id'         => 'desc',
                                    'curr_grad_start' => 'desc',
                                    'count_wins'      => 'desc'
                                ]);
            } else {
                $db = Db::name('user')
                        ->alias('u')
                        ->field('u.id,city,user_grad_logo,u.display_name as user_name,avatar,grad.name,grad.id as g_id,curr_grad_start')
                        ->join('grad', 'grad.id=u.grad_id')
                        ->order([
                                    'grad_id'         => 'desc',
                                    'curr_grad_start' => 'desc',
                                    'count_wins'      => 'desc'
                                ])
                        ->limit(0, 100);

            }
            $list = $db->select();
            if ($list) {
                //第一名,第二名,第三名
                $one     = [];
                $two     = [];
                $three   = [];
                $lists   = [];
                $my_sort = [];
                foreach ($list as $key => $value) {
                    if ($this->user_info['id'] === $value['id']) {
                        $my_sort          = $value;
                        $my_sort['index'] = $key + 1;
                    }
                    $lists[$key + 1] = $value;
                }
                if (isset($lists[1])) {
                    $one = $lists[1];
                }
                if (isset($lists[2])) {
                    $two = $lists[2];
                }
                if (isset($lists[3])) {
                    $three = $lists[3];
                }
                unset($lists[1], $lists[2], $lists[3]);
                return $this->json_return(1, '排行榜列表获取成功', [
                    'one'     => $one,
                    'two'     => $two,
                    'three'   => $three,
                    'list'    => $lists,
                    'my_sort' => $my_sort
                ]);
            }
            return $this->json_error('暂无排行数据');
        } else {
            return $this->json_error('排行榜获取失败,客服端错误');
        }
    }


    /**
     * 上个赛季排行
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOldSort()
    {
        $list = Db::name('user')
                  ->alias('u')
                  ->field('u.id,city,user_grad_logo,u.display_name as user_name,avatar,grad.name,grad.id as g_id,s.curr_grad_start')
                  ->join('season_ranking_log s', 'u.id=s.`user_id`')
                  ->join('grad', 'grad.id=s.grad_id')
                  ->order([
                              's.grad_id'         => 'desc',
                              's.curr_grad_start' => 'desc',
                              's.count_wins'      => 'desc'
                          ])
                  ->select();
        if ($list) {
            //第一名,第二名,第三名
            $one     = [];
            $two     = [];
            $three   = [];
            $lists   = [];
            $my_sort = [];
            foreach ($list as $key => $value) {
                if ($this->user_info['id'] === $value['id']) {
                    $my_sort          = $value;
                    $my_sort['index'] = $key + 1;
                }
                $lists[$key + 1] = $value;
            }
            if (isset($lists[1])) {
                $one = $lists[1];
            }
            if (isset($lists[2])) {
                $two = $lists[2];
            }
            if (isset($lists[3])) {
                $three = $lists[3];
            }
            unset($lists[1], $lists[2], $lists[3]);
            return $this->json_return(1, '排行榜列表获取成功', [
                'one'     => $one,
                'two'     => $two,
                'three'   => $three,
                'list'    => $lists,
                'my_sort' => $my_sort
            ]);
        }
        return $this->json_error('暂无数据');
    }
}